package com.xianyun.rpc.server.impl;

import com.xianyun.rpc.server.HttpServer;
import io.vertx.core.Vertx;

/**
 * @author xianYun
 * @version 1.0
 **/
public class VertxHttpServer implements HttpServer {
    public void doStart(int port) {
        //创建 Vertx 实例
        Vertx vertx = Vertx.vertx();
        //创建 HTTP 服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();
        //region 简单处理请求，验证服务器是否搭建成功
        //server.requestHandler(request -> {
        //    //处理请求
        //    System.out.println("Received request: " + request.uri() + " " + request.method());
        //    //响应请求
        //    request.response()
        //            .putHeader("content-type", "text/plain")
        //            .end("Hello from Vert.x HTTP server!");
        //});
        //endregion

        //region 绑定自定义的请求服务器：根据请求参数找到对应的实现类并调用对应的方法。
        server.requestHandler(new HttpServerHandler());
        //endregion

        //启动 HTTP 服务器并监听指定端口
        server.listen(port, result -> {
            if (result.succeeded()) {
                System.out.println("HTTP server started on port " + port);
            } else {
                System.out.println("Failed to start HTTP server: " + result.cause());
            }
        });
    }
}
